﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;

namespace _5ChickDeal.UserControl
{
    public partial class Registration : System.Web.UI.UserControl
    {
        DataClassesDataContext db;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DataClassesDataContext();
            if (IsPostBack)
            {
                ButtonClick();
            }
        }
        private int GetMaxIDKhachHang()
        {
            int id = 0;
            try
            {
                id = db.KhachHangs.Max(kh => kh.id);
            }
            catch { }
            return id;
        }
       

        protected void ButtonClick()
        {
            KhachHang kh = new KhachHang();
            kh.id = GetMaxIDKhachHang() + 1;
            kh.email = TextBoxEmail.Text;
            kh.hoten = TextBoxHoTen.Text;
            kh.matkhau = EncoderPassword.GetMd5Hash( TextBoxPassword.Text);
            kh.dienthoai = TextBoxDienThoai.Text;
            try
            {
                db.KhachHangs.InsertOnSubmit(kh);
                db.SubmitChanges();
                Session["user"] = kh;
                Response.Redirect("~/Default.aspx");
            }
            catch
            {
            }
        }
    }
}